﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DimsMapper.Modules
{
    static class FrmFiles
    {
        static readonly string[] artPath = {
            "art\\items\\",
            "art\\critters\\",
            "art\\scenery\\",
            "art\\walls\\",
            "art\\tiles\\",
            "art\\misc\\",
            "art\\intrface\\",
            "art\\inven\\"
        };

        static Dictionary<int, FrmImage>[] frmList = new Dictionary<int, FrmImage>[8];

        static public Palette FrmPal { get; private set; }

        public static void Initialization()
        {
            if (frmList[0] != null) return;

            DBFile file = DatFiles.OpenMapperFile("color.pal");
            if (file == null) {
                file = DatFiles.OpenFile("color.pal");
                if (file == null) {
                    // ошибка
                }
            }

            FrmPal = new Palette(file);
            file.Close();

            for (int i = 0; i < 8; i++)
            {
                frmList[i] = new Dictionary<int, FrmImage>();
            }

            LoadingLocalFRM();
        }

        static void LoadingLocalFRM()
        {
            LoadIntefaceFRM("Hex.frm", (int)Main.ShowTypes.HexGrid);
            LoadIntefaceFRM("HexObj.frm", (int)Main.ShowTypes.HexGridObj);
            LoadIntefaceFRM("SelfBlockFull.frm", (int)Main.ShowTypes.HexBlock);
            LoadIntefaceFRM("SelfBlock.frm", (int)Main.ShowTypes.HexBlockThru);
        }

        public static void Clear()
        {
            for (int i = 0; i < 8; i++)
            {
                if (i == 6) continue;
                frmList[i].Clear();
            }
        }

        static FrmImage loadFRM(Object.Types type, int index, int artFid)
        {
            string filename = LstFiles.FrmNameFile((int)type, index);
            if (filename == null)
                return null;

            if (type == Object.Types.Critter) {
                Utilites.GetCritterFileName(ref filename, artFid);
                index = artFid; // заменяем ключ
            }

            string filePath = artPath[(int)type] + filename;
            DBFile file = DatFiles.OpenFile(filePath);
            if (file == null) {
                // ошибка
                return null;
            }

            FrmImage frm = new FrmImage(file);
            file.Close();

            frmList[(int)type].Add(index, frm);
            return frm;
        }

        public static bool LoadIntefaceFRM(string frmFile, int index)
        {
            DBFile file = DatFiles.OpenMapperFile(frmFile);
            if (file == null) return false;

            FrmImage frm = new FrmImage(file);
            file.Close();

            frmList[(int)Object.Types.Interface].Add(index, frm);
            return true;
        }

        public static bool LoadTileFRM(int fid)
        {
            if (frmList[(int)Object.Types.Tiles].ContainsKey(fid))
                return true;

            return loadFRM(Object.Types.Tiles, fid, 0) != null;
        }

        public static bool LoadFRM(int fid)
        {
            Object.Types type = (Object.Types)((fid & 0x0F000000) >> 24);
            int index = (type == Object.Types.Tiles) ? fid & 0x3FFF : fid & 0xFFF;

            int indexKey = (type == Object.Types.Critter) ? fid : index;

            if (frmList[(int)type].ContainsKey(indexKey)) return true;

            return loadFRM(type, index, fid) != null;
        }

        public static FrmImage GetFRM(Object.Types type, int index)
        {
            FrmImage frm;
            if (frmList[(int)type].TryGetValue(index, out frm)) return frm;

            return loadFRM(type, index, 0);
        }

        public static FrmImage GetFRM(int fid)
        {
            Object.Types type = (Object.Types)((fid & 0x0F000000) >> 24);
            int index = (type == Object.Types.Tiles) ? fid & 0x3FFF : fid & 0xFFF;
        
            int indexKey = (type == Object.Types.Critter) ? fid : index;

            FrmImage frm;
            if (frmList[(int)type].TryGetValue(indexKey, out frm)) return frm;

            return loadFRM(type, index, fid);
        }
    }
}
